<template>
  <div class="student-account-container">
    <div class="header">
      <div class="left">
        <h2>学生账户管理</h2>
      </div>
      <div class="right">
        <!-- 搜索框 -->
        <el-input
          v-model="searchQuery"
          placeholder="搜索学生姓名"
          style="width: 220px"
          clearable
          @clear="handleSearch"
          @keyup.enter="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </div>
    </div>

    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="accounts"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column label="学生姓名" width="120">
          <template #default="{ row }">
            {{ row.student.name }}
          </template>
        </el-table-column>
        <el-table-column prop="bankName" label="开户银行" width="150" />
        <el-table-column prop="bankCardNumber" label="银行卡号" min-width="180" />
        <el-table-column prop="alipayAccount" label="支付宝账号" min-width="180" />
        <el-table-column prop="wechatAccount" label="微信账号" min-width="180" />
        <el-table-column label="更新时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button
              type="danger"
              size="small"
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'AdminStudentAccountList'
}
</script>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import { getStudentAccounts, deleteStudentAccount } from '@/api/admin/studentAccount'
import { formatDate } from '@/utils/format'

// 表格数据
const loading = ref(false)
const accounts = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const searchQuery = ref('')

// 获取账户列表
const fetchAccounts = async () => {
  try {
    loading.value = true
    const response = await getStudentAccounts({
      page: currentPage.value - 1,
      size: pageSize.value,
      keyword: searchQuery.value
    })
    
    if (response) {
      accounts.value = response.content
      total.value = response.totalElements
    }
  } catch (error) {
    console.error('Failed to fetch accounts:', error)
    ElMessage.error('获取账户列表失败')
  } finally {
    loading.value = false
  }
}

// 处理搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchAccounts()
}

// 处理分页
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchAccounts()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchAccounts()
}

// 处理删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除这个学生账户吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteStudentAccount(row.id)
      ElMessage.success('删除成功')
      fetchAccounts()
    } catch (error) {
      console.error('Delete failed:', error)
      ElMessage.error(error.response?.data || '删除失败')
    }
  })
}

// 在组件挂载时获取数据
onMounted(() => {
  fetchAccounts()
})
</script>

<style scoped>
.student-account-container {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 16px 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.left {
  display: flex;
  align-items: center;
  gap: 16px;

  h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #303133;
  }
}

.right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.table-card {
  margin-bottom: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  padding: 16px 20px;
  display: flex;
  justify-content: flex-end;
  background-color: #fff;
  border-top: 1px solid #ebeef5;
}

/* 调整表格内容垂直对齐 */
:deep(.el-table .cell) {
  display: flex;
  align-items: center;
}

/* 优化按钮样式 */
:deep(.el-button--small) {
  padding: 8px 16px;
}
</style> 